package LogisticsSystem.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ReturnRowsClause;

import LogisticsSystem.po.Customer;
import LogisticsSystem.po.Orders;
import LogisticsSystem.po.Orderdetail;
import LogisticsSystem.po.Receiver;
import LogisticsSystem.po.Cargo;
import LogisticsSystem.service.CargoService;
import LogisticsSystem.service.CustomerService;
import LogisticsSystem.service.DispatcherService;

@Controller
@RequestMapping("customer")
public class CustomerController {

	@Autowired
	private CargoService cargoService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private DispatcherService dispatcherService;
	private Cargo cargo;

	// 1.修改个人信息
	@RequestMapping("/ustore")
	public String updateUser(Model model, HttpServletRequest request) throws Exception {
		Customer customer = (Customer) request.getSession().getAttribute("onlineUser");
		String n_pwd = request.getParameter("n_pwd");
		boolean isTrue = customer.getcPwd().equals(request.getParameter("c_pwd"));
		boolean isEqual = n_pwd.equals(request.getParameter("n_pwd2"));
		if (isEqual && isTrue) {
			customer.setcName(request.getParameter("c_name"));
			customer.setcPwd(n_pwd);
			if (customerService.updateCustomer(customer)) {
				request.getSession().setAttribute("tip", "修改成功");
				request.getSession().setAttribute("onlineUser", customer);
				return "redirect:/index.jsp";// 返回该顾客的主页
			}
		}
		model.addAttribute("error", "error");
		if (isTrue) {
			model.addAttribute("tip", "原密码错误");
		} else if (isEqual) {
			model.addAttribute("tip", "两次输入密码不一致");//
		} else {
			model.addAttribute("tip", "未知错误");//
		}
		return "redirect:/index.jsp";//
	}

	// 2.修改联系人
	@RequestMapping("/rstore")
	public String updateReciver(Model model, HttpServletRequest request) throws Exception {
		Customer customer = (Customer) request.getSession().getAttribute("onlineUser");
		Receiver receiver = new Receiver();
		receiver.setcId(customer.getcId());
		receiver.setrId(Integer.valueOf(request.getParameter("r_id")));
		receiver.setrName(request.getParameter("r_name"));
		receiver.setrPhone(request.getParameter("r_phone"));
		receiver.setrPrivince(request.getParameter("r_privince"));
		receiver.setrCity(request.getParameter("r_city"));
		receiver.setrAddress(request.getParameter("r_address"));

		if (customerService.updateReciver(receiver)) {
			request.getSession().setAttribute("tip", "修改成功");
			request.getSession().setAttribute("recevierList", customerService.showReceiver(customer.getcId()));
			return "redirect:/index.jsp";// 返回该顾客的主页 } }
		}
		model.addAttribute("error", "error");
		return "redirect:/index.jsp";//
	}

	// 3.添加联系人
	@RequestMapping("/addreceiver")
	public String insertReceiver(Model model, HttpServletRequest request) throws Exception {
		Customer customer = (Customer) request.getSession().getAttribute("onlineUser");
		Receiver receiver = new Receiver();
		receiver.setrName(request.getParameter("r_name"));
		receiver.setrPhone(request.getParameter("r_phone"));
		receiver.setrPrivince(request.getParameter("r_privince"));
		receiver.setrCity(request.getParameter("r_city"));
		receiver.setrAddress(request.getParameter("r_address"));
		receiver.setcId(customer.getcId());

		if (receiver != null && customerService.selectCustomer(receiver.getcId()) != null) {
			if (receiver.getrAddress() != null && receiver.getrCity() != null && receiver.getrName() != null
					&& receiver.getrPhone() != null && receiver.getrPrivince() != null
					&& customerService.insertReciver(receiver)) {
				model.addAttribute("tip", "新增成功");
				request.getSession().setAttribute("recevierList", customerService.showReceiver(customer.getcId()));
				return "redirect:/index.jsp";// 返回联系人列表
			}
			model.addAttribute("error", "error");
			model.addAttribute("tip", "信息未填写完整");
		}
		return "redirect:/index.jsp";//
	}

	// 4.删除联系人，传入删除的联系人的ID
	@RequestMapping("/deleteReceiver")
	public String deleteReceiver(HttpServletRequest request) throws Exception {
		Customer customer = (Customer) request.getSession().getAttribute("onlineUser");
		Integer id = Integer.valueOf(request.getParameter("rId"));
		System.out.println(id);
		if (id != null && customerService.deleteReciver(id)) {
			request.getSession().setAttribute("recevierList", customerService.showReceiver(customer.getcId()));
			return "redirect:/index.jsp";// 返回联系人列表
		}
		request.getSession().setAttribute("error", "error");
		request.getSession().setAttribute("tip", "删除失败");
		return "redirect:/index.jsp";// 返回联系人列表
	}

	// 5.填写派送请求，传入当前顾客id，选择联系人，选择商品和数量，
	@RequestMapping("/orderStore")
	public String insertOrder(HttpServletRequest request, Model model) throws Exception {
		Integer rid = Integer.valueOf((String) request.getParameter("contactPerson"));
		Orderdetail orderdetail = new Orderdetail();
		Double CargoNumber = Double.valueOf((String) request.getParameter("goodsNumber"));

		orderdetail.setCargoNumber(CargoNumber);
		orderdetail.setCargoId(Integer.valueOf((String) request.getParameter("goodsId")));
		System.out.println(Integer.valueOf((String) request.getParameter("goodsId")));
		orderdetail.setoId(123);

		if (rid != null && orderdetail != null) {
			Customer customer = (Customer) request.getSession().getAttribute("onlineUser");
			Orders order = new Orders();
			order.setcId(customer.getcId());
			order.setoDate(new Date());
			order.setoFeetype((String) request.getParameter("goodsPrice"));
			order.setoStatus(0);// 0代表为配送请求
			order.setoRId(rid);
			order.setoId(null);

			Cargo cargo = cargoService.getoneCargo(orderdetail.getCargoId());
			cargo.setCargoCount(cargo.getCargoCount() - CargoNumber);

			if (customerService.insertOrder(order, orderdetail) && cargoService.updateCargo(cargo) > 0) {
				model.addAttribute("tip", "生成配送请求成功");
				request.getSession().setAttribute("CargoList", cargoService.getCargolist(null));
				return "redirect:/index.jsp";// 返回顾客首页
			}
		}
		model.addAttribute("error", "error");
		model.addAttribute("tip", "信息未填写完整");

		return "redirect:/index.jsp";// 返回填写页面
	}

}
